/**
 * @Author: Bobby
 * @Date: 2022-01-07 20:38:19
 * @LastEditTime: 2022-01-08 12:01:09
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\common\project\bootstrap\microApp.js
 * @CIMC-V2
 */

import { registerMicroApps, addGlobalUncaughtErrorHandler, initGlobalState, start } from 'qiankun'
// 微前端(主应用)
export function microStart(apps) {
  console.log('microStart:', apps)
  registerMicroApps(apps, {
    beforeLoad: [
      (app) => {
        console.log('before load', app.name)
        return Promise.resolve()
      },
    ],
    beforeMount: [
      (app) => {
        console.log('before mount', app.name)
        return Promise.resolve()
      },
    ],
    afterUnmount: [
      (app) => {
        console.log('after mount', app.name)
        return Promise.resolve()
      },
    ],
  })
  const state = {}
  //主项目与子项目交互用的参数，子项目与主项目都可以修改此参数
  const actions = initGlobalState(state)
  actions.setGlobalState({
    globalToken: '',
  })
  addGlobalUncaughtErrorHandler((event) => {
    console.log(event)
    const { msg } = event
    if (msg && msg.includes('died in status LOADING_SOURCE_CODE')) {
      console.log('微应用加载失败，请检查应用是否可运行')
    }
  })
  start()
  return actions
}
